package com.icode.service.impl.house;

import com.icode.core.dto.house.HouseInfoDTO;
import com.icode.core.dto.house.HouseWeekDTO;
import com.icode.core.model.house.*;
import com.icode.core.shared.DataToJson;
import com.icode.dao.HouseDao;
import com.icode.dao.HouseWeekDao;
import com.icode.service.house.HouseWeeklyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-6-29
 * Time: 下午3:49
 * To change this template use File | Settings | File Templates.
 */
@Service("houseWeeklyService")
@Transactional
public class HouseWeeklyServiceImpl implements HouseWeeklyService {

    @Autowired
    private HouseWeekDao houseWeekDao;

    @Autowired
    private HouseDao houseDao;

    @Override
    @Transactional
    public void findHouseInfoByArea(String area, HttpServletResponse response) {
        List<HouseInfo> houseInfos = houseWeekDao.findHouseInfoByArea(area);
        List<HouseInfoDTO> houseInfoDTOs = HouseInfoDTO.toDTOs(houseInfos);
        DataToJson.houseInfoByAreaToJson(houseInfoDTOs, response);
    }

    @Override
    @Transactional
    public void findHouseInfoForHouseWeekTime(String area, HttpServletResponse response) {
        List<HouseInfo> houseInfos = houseWeekDao.findHouseInfoByArea(area);
        List<HouseInfoDTO> houseInfoDTOs = HouseInfoDTO.toDTOs(houseInfos);
        DataToJson.houseInfoForHouseWeekTime(houseInfoDTOs, response);
    }

    @Override
    @Transactional
    public void divideHouseWeek(String area, String guids, String week, String price, String description) {
        String[] guid = guids.split(",");
        for (int i = 0; i < guid.length; i++) {
            HouseInfo houseInfo = houseWeekDao.findByGuid(HouseInfo.class, guid[i]);
            HouseWeek find = houseWeekDao.findHouseWeekByWeek(week, houseInfo);
            if (find == null) {
                find = new HouseWeek(week, description, price, HouseBaseArea.valueOf(area), houseInfo);
                houseWeekDao.saveOrUpdate(find);
            } else {
                find.initHouseWeek(week, price, HouseBaseArea.valueOf(area), houseInfo);
            }
        }
    }

    @Override
    @Transactional
    public void findHouseWeekByArea(List<HouseWeekDTO> houseWeekDTOs, HttpServletResponse response) {
        DataToJson.houseWeekByAreaToJson(houseWeekDTOs, response);
    }

    @Override
    @Transactional
    public void findHouseWeekTimeByArea(String area, HttpServletResponse response) {
        List<HouseWeekTime> houseWeekTimes = houseWeekDao.findHouseWeekTimeByArea(area);
        DataToJson.houseWeekTimeToJson(houseWeekTimes, response);
    }

    @Override
    @Transactional
    public void deleteHouseWeekByGuids(String guids) {
        String[] guid = guids.split(",");
        for (int i = 0; i < guid.length; i++) {
            HouseWeek houseWeek = houseWeekDao.findByGuid(HouseWeek.class, guid[i]);
            houseWeekDao.deleteObject(houseWeek);
        }
    }

    @Override
    @Transactional
    public void findHouseWeekType(String area, String houseInfoGuid, HttpServletResponse response) {
        HouseInfo houseInfo = houseWeekDao.findByGuid(HouseInfo.class, houseInfoGuid);
        List<HouseWeek> houseWeeks = houseWeekDao.findHouseWeekType(area, houseInfo);
        List<HouseWeekDTO> houseWeekDTOs = HouseWeekDTO.toDTOs(houseWeeks);
        DataToJson.houseWeekTypeToJson(houseWeekDTOs, response);
    }

    @Override
    @Transactional
    public void divideWeekTime(String houseWeekGuid, String time) {
        HouseWeek houseWeek = houseWeekDao.findByGuid(HouseWeek.class, houseWeekGuid);
        String[] times = time.split(";");
//        houseWeekDao.deleteHouseWeekTimeByHouseWeek(houseWeek);
        for (int i = 0; i < times.length; i++) {
            String[] months = times[i].split(",")[0].split("-");
            int month = Integer.parseInt(months[1]);
            HouseWeekTime houseWeekTime = new HouseWeekTime(month, times[i].split(",")[0], times[i].split(",")[1], true, houseWeek);
            houseWeekDao.saveOrUpdate(houseWeekTime);
        }
    }

    @Override
    @Transactional
    public void deleteHouseWeekTimeByGuids(String guids) {
        String[] guid = guids.split(",");
        for (int i = 0; i < guid.length; i++) {
            HouseWeekTime houseWeekTime = houseWeekDao.findByGuid(HouseWeekTime.class, guid[i]);
            houseWeekDao.deleteObject(houseWeekTime);
        }
    }

    @Override
    public List<HouseInfoDTO> findAllHouseInfos() {
        List<HouseInfoDTO> houseInfoDTOs = new ArrayList<>();
        List<HouseInfo> houseInfos = houseWeekDao.findAll(HouseInfo.class);
        for (HouseInfo houseInfo : houseInfos) {
            String houseName = houseInfo.baseInfo().houseName();
            String guid = houseInfo.guid();
            HouseInfoDTO houseInfoDTO = new HouseInfoDTO(houseName, guid);
            houseInfoDTOs.add(houseInfoDTO);
        }
        return houseInfoDTOs;
    }

    @Override
    public List<HouseWeekDTO> findHouseWeekByHouseInfoGuid(String guid) {
        List<HouseWeek> houseWeeks = houseWeekDao.findHouseWeekByHouseInfoGuid(guid);
        return HouseWeekDTO.toDTOs(houseWeeks);
    }

    @Override
    public HouseWeekDTO findHouseWeekByGuid(String houseWeekGuid) {
        HouseWeek houseWeek = houseWeekDao.findByGuid(HouseWeek.class, houseWeekGuid);
        return HouseWeekDTO.toDTO(houseWeek);
    }

    @Override
    @Transactional
    public void upDateBase(HouseWeekDTO houseWeekDTO) {
        HouseWeek houseWeek = houseWeekDao.findByGuid(HouseWeek.class, houseWeekDTO.getGuid());
        houseWeek.upDateBase(houseWeekDTO);
    }

    @Override
    @Transactional
    public void updatehouseweektime(String start, String end, String houseweektimeguid) {
        HouseWeekTime houseWeekTime = houseWeekDao.findByGuid(HouseWeekTime.class, houseweektimeguid);
        String[] time = start.split("-");
        String month = time[1];
        houseWeekTime.updateStartAndEnd(start, end,month);
    }

    @Override
    @Transactional
    public void addweek(String houseInfoguid, String week, String price, String description, String totalNum) {
        HouseInfo houseInfo = houseWeekDao.findByGuid(HouseInfo.class, houseInfoguid);
        HouseBaseArea houseBaseArea = houseInfo.buildingType().houseBase().houseBaseArea();
        week = weekEnToCh(week);
        List<HouseWeek> houseWeeks = houseInfo.houseWeek();
        boolean b = true;
        for (HouseWeek houseWeek : houseWeeks) {
            if (houseWeek.name().equals(week)) {
                HouseWeek findhouseWeek = houseWeekDao.findHouseWeekByWeek(week, houseInfo);
                findhouseWeek.upDateBase(price, description, totalNum);
                b = false;
            }
        }
        if (true == b) {
            HouseWeek houseWeek = new HouseWeek(houseInfo, week, price, description, totalNum, houseBaseArea);
            houseWeekDao.saveOrUpdate(houseWeek);
        }
    }

    @Override
    public HouseWeekDTO findHouseWeekByHouseInfoAndweekName(String houseInfoGuid, String week) {
        HouseInfo houseInfo = houseDao.findByGuid(HouseInfo.class, houseInfoGuid);
        week = weekEnToCh(week);
        HouseWeek houseWeek = houseWeekDao.findHouseWeekByWeek(week, houseInfo);
        if (houseWeek == null) {
            return null;
        }
        HouseWeekDTO houseWeekDTO = HouseWeekDTO.toDTO(houseWeek);
        return houseWeekDTO;
    }

    @Override
    public void addweeks(String guids, String weekH, String weekW, String weekP, String weekD) {
        String[] guid = guids.split(",");
        String[] weektimeH = weekH.split(";");
        String[] weektimeW = weekW.split(";");
        String[] weektimeP = weekP.split(";");
        String[] weektimeD = weekD.split(";");
        for (String houseInfoguid : guid) {
            HouseInfo houseInfo = houseDao.findByGuid(HouseInfo.class, houseInfoguid);
            save(weektimeH,"黄金周",houseInfo);
            save(weektimeW,"旺季周",houseInfo);
            save(weektimeP,"平季周",houseInfo);
            save(weektimeD,"淡季周",houseInfo);
        }
    }

    @Override
    @Transactional
    public void batchupdatehouseweektime(String houseInfoguids, String weekName, String weekTimes) {
        String[] guids = houseInfoguids.split(",");
        weekName = weekEnToCh(weekName);
        for (String guid : guids) {
            HouseInfo houseInfo = houseDao.findByGuid(HouseInfo.class, guid);
            HouseWeek houseWeek = houseWeekDao.findHouseWeekByWeek(weekName, houseInfo);
          //  houseWeekDao.deleteHouseWeekTimeByHouseWeek(houseWeek);
            String[] times = weekTimes.split(";");
            for (int i = 0; i < times.length; i++) {
                String[] months = times[i].split(",")[0].split("-");
                int month = Integer.parseInt(months[1]);
                HouseWeekTime houseWeekTime = new HouseWeekTime(month, times[i].split(",")[0], times[i].split(",")[1], true, houseWeek);
                houseWeekDao.saveOrUpdate(houseWeekTime);
            }
        }
    }

    public String weekEnToCh(String weekName) {
        switch (weekName) {
            case "HUANGJINZHOU":
                weekName = "黄金周";
                break;
            case "WANGJIZHOU":
                weekName = "旺季周";
                break;
            case "PINGJIZHOU":
                weekName = "平季周";
                break;
            case "DANJIZHOU":
                weekName = "淡季周";
                break;
        }
        return weekName;
    }

    private  void save(String[] weektime,String weekName ,HouseInfo houseInfo) {
        if (weektime.length > 0) {
            HouseWeek houseWeek = houseWeekDao.findHouseWeekByWeek(weekName, houseInfo);
            houseWeekDao.deleteHouseWeekTimeByHouseWeek(houseWeek);
            houseWeekDao.saveOrUpdate(houseWeek);
            for (int i = 0; i < weektime.length; i++) {
                String[] months = weektime[i].split(",")[0].split("-");
                int month = Integer.parseInt(months[1]);
                HouseWeekTime houseWeekTime = new HouseWeekTime(month, weektime[i].split(",")[0], weektime[i].split(",")[1], true, houseWeek);
                houseWeekDao.saveOrUpdate(houseWeekTime);
            }
        }
    }
}
